package tbalert.web.action;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import tbalert.hibernate3.Finder;
import tbalert.page.Pagination;
import tbalert.session.SessionProvider;
import tbalert.web.FrontConstants;
import tbalert.web.SysConstants;
import tbalert.web.WebErrors;
import tbalert.web.entity.BaseInfo;
import tbalert.web.entity.Dwdm;
import tbalert.web.entity.Role;
import tbalert.web.entity.Ymjg;
import tbalert.web.manager.BrxxMng;
import tbalert.web.manager.YmjgMng;

@Controller
@RequestMapping(value = "/ymjg")
public class YmjgAct {
	private static final Logger log = LoggerFactory.getLogger(DwdmAct.class);
	
	@RequestMapping(value = "/list.do")
	public String list(ModelMap model, HttpServletRequest request, String kicid, 
			String djrqq, String djrqz, String tbrqq, String tbrqz, String ym_inh, String ym_rfp, String ym_emb, String ym_sm, 
			Integer pageNo, Integer pageSize) {
		if (pageSize == null) {
			pageSize = 20;
		}
		if (pageNo == null) {
			pageNo = 1;
		}
		Dwdm dwdm = (Dwdm) session.getAttribute(request, SysConstants.DWDM_KEY);
		String sjdwId = dwdm.getDwid();
		if (!StringUtils.isBlank(sjdwId) && (sjdwId.equals("00") || sjdwId.equals("bssa"))) {
			sjdwId = "";
		}
		Finder finder = Finder.create("from BaseInfo b, Ymjg y where b.kicid=y.kicid and b.djdw like '" + sjdwId + "%'");
		if (!StringUtils.isBlank(kicid)) {
			finder.append(" and b.kicid like '%" + kicid + "%'");
		}
		if (!StringUtils.isBlank(djrqq)) {
			finder.append(" and b.djrq >= '" + djrqq + " 00:00:00'");
		}
		if (!StringUtils.isBlank(djrqz)) {
			finder.append(" and b.djrq <= '" + djrqz + " 23:59:59'");
		}
		if (!StringUtils.isBlank(tbrqq)) {
			finder.append(" and y.tbrq >= '" + tbrqq + " 00:00:00'");
		}
		if (!StringUtils.isBlank(tbrqz)) {
			finder.append(" and y.tbrq <= '" + tbrqz + " 23:59:59'");
		}
		
		String hql = "";
		if (!StringUtils.isBlank(ym_inh)) {
			hql = "(y.ym_inh = '" + ym_inh + "')";
		}
		if (!StringUtils.isBlank(ym_rfp)) {
			if(!StringUtils.isBlank(hql)){
				hql = hql.substring(0, hql.length()-1) + " or y.ym_rfp = '" + ym_rfp + "')";
			}else{
				hql = "(y.ym_rfp = '" + ym_rfp + "')";
			}
		}
		if (!StringUtils.isBlank(ym_emb)) {
			if(!StringUtils.isBlank(hql)){
				hql = hql.substring(0, hql.length()-1) + " or y.ym_emb = '" + ym_emb + "')";
			}else{
				hql = "(y.ym_emb = '" + ym_emb + "')";
			}
		}
		if (!StringUtils.isBlank(ym_sm)) {
			if(!StringUtils.isBlank(hql)){
				hql = hql.substring(0, hql.length()-1) + " or y.ym_sm = '" + ym_sm + "')";
			}else{
				hql = "(y.ym_sm= '" + ym_sm + "')";
			}
		}
		if (!StringUtils.isBlank(hql)) {
			hql = " and " + hql;
		}
		finder.append(hql + " and b.submit=1");
		Pagination p = brxxMng.findByFinder(finder, pageNo, pageSize);
		model.put("pagination", p);
		model.put("kicid", kicid);
		model.put("djrqq", djrqq);
		model.put("djrqz", djrqz);
		model.put("tbrqq", tbrqq);
		model.put("tbrqz", tbrqz);
		model.put("ym_inh", ym_inh);
		model.put("ym_rfp", ym_rfp);
		model.put("ym_emb", ym_emb);
		model.put("ym_sm", ym_sm);
		return "/ymjg/list";
	}

	@RequestMapping(value = "/add.do")
	public String add(ModelMap model, HttpServletRequest request, String kicid) {
		if (!StringUtils.isBlank(kicid)) {
			Dwdm dw = (Dwdm) session.getAttribute(request, SysConstants.DWDM_KEY);
			BaseInfo baseInfo = brxxMng.findByKicid(kicid);
			Ymjg ymjg = ymjgMng.findById(kicid);
			if (baseInfo == null) {
				model.put("tips", "该病人信息卡不存在");
			} else if (!StringUtils.startsWith(baseInfo.getDjdw().getDwid(), dw.getDwid()) && !StringUtils.equals(dw.getDwid(), "00") && !StringUtils.equals(dw.getDwid(), "bssa")) {
				model.put("tips", "您无权录入该病人｛" + baseInfo.getBrxm() + "｝的药敏结果信息");
			} else if (!baseInfo.getSubmit()) {
				model.put("tips", "该病人信息卡尚未提交");
			} else {
				model.put("baseInfo", baseInfo);
				model.put("constants", constants);
				if (ymjg != null) {
					model.put("ymjg", ymjg);
					return "/ymjg/edit";
				}
			}
			model.put("kicid", kicid);
		}
		return "/ymjg/add";
	}

	@RequestMapping(value = "/view.do")
	public String view(ModelMap model, HttpServletRequest request, String kicid) {
		if (!StringUtils.isBlank(kicid)) {
			Dwdm dw = (Dwdm) session.getAttribute(request, SysConstants.DWDM_KEY);
			BaseInfo baseInfo = brxxMng.findByKicid(kicid);
			Ymjg ymjg = ymjgMng.findById(kicid);
			if (baseInfo == null) {
				model.put("tips", "该病人信息卡不存在");
			} else if (!StringUtils.startsWith(baseInfo.getDjdw().getDwid(), dw.getDwid()) && !StringUtils.equals(dw.getDwid(), "00") && !StringUtils.equals(dw.getDwid(), "bssa")) {
				model.put("tips", "您无权查看该病人｛" + baseInfo.getBrxm() + "｝的药敏结果");
			} else if (!baseInfo.getSubmit()) {
				model.put("tips", "该病人信息卡尚未提交");
			} else if (ymjg == null) {
				model.put("tips", "该病人的药敏结果不存在！");
			} else {
				model.put("baseInfo", baseInfo);
				model.put("ymjg", ymjg);
			}
			model.put("kicid", kicid);
			model.put("constants", constants);
		}
		return "/ymjg/view";
	}

	@RequestMapping(value = "/save.do")
	public String save(ModelMap model, HttpServletRequest request, Ymjg ymjg) {
		WebErrors errors = validateSave(ymjg, request);
		if (errors.hasErrors()) {
			return errors.showErrorPage(model);
		}
		Dwdm dw = (Dwdm) session.getAttribute(request, SysConstants.DWDM_KEY);
		ymjg.setTbdw(dw);
		ymjgMng.save(ymjg);
		model.put(SysConstants.MESSAGE, "global.success");
		return add(model, request, null);
	}

	@RequestMapping(value = "/update.do")
	public String update(ModelMap model, HttpServletRequest request, Ymjg ymjg) {
		WebErrors errors = validateUpdate(ymjg.getKicid(), request);
		if (errors.hasErrors()) {
			return errors.showErrorPage(model);
		}
		ymjgMng.update(ymjg);
		model.put(SysConstants.MESSAGE, "global.success");
		return this.add(model, request, null);
	}

	@RequestMapping(value = "/delete.do")
	public String delete(ModelMap model, HttpServletRequest request, String kicid) {
		Ymjg ymjg = ymjgMng.deleteById(kicid);
		model.put(SysConstants.MESSAGE, "global.success");
		return "redirect:welcome.do";
	}

	@Autowired(required = true)
	private YmjgMng ymjgMng;
	@Autowired(required = true)
	private BrxxMng brxxMng;
	@Autowired(required = true)
	private FrontConstants constants;
	@Autowired(required = true)
	private SessionProvider session;

	private WebErrors validateSave(Ymjg bean, HttpServletRequest request) {
		WebErrors errors = new WebErrors(request);
		return errors;
	}

	private WebErrors validateEdit(String kicid, HttpServletRequest request) {
		WebErrors errors = new WebErrors(request);
		if (vldExist(kicid, errors)) {
			return errors;
		}
		return errors;
	}

	private WebErrors validateUpdate(String kicid, HttpServletRequest request) {
		WebErrors errors = new WebErrors(request);
		if (vldExist(kicid, errors)) {
			return errors;
		}
		return errors;
	}

	private boolean vldExist(String kicid, WebErrors errors) {
		if (errors.ifNull(kicid, "kicid")) {
			return true;
		}
		Ymjg entity = ymjgMng.findById(kicid);
		if (errors.ifNotExist(entity, Role.class, kicid)) {
			return true;
		}
		return false;
	}
}
